package com.jhhc.EngineeringSafety.BaseInfo;

import com.jhhc.EngineeringSafety.BaseInfo.entity.BDMS_ES_FORECASTOBJ_B;
import com.jhhc.EngineeringSafety.BaseInfo.entity.result.*;
import com.jhhc.EngineeringSafety.BaseInfo.service.BDMS_ES_FORECASTOBJ_BService;
import com.jhhc.EngineeringSafety.Calculate.Forecast.entity.BDMS_YB_RESULT_M;
import com.jhhc.EngineeringSafety.Calculate.Forecast.entity.result.Data;
import com.jhhc.EngineeringSafety.Calculate.Forecast.entity.result.ModelData;
import com.jhhc.EngineeringSafety.Calculate.Forecast.entity.result.ValueModelC;
import com.jhhc.FloodForecast.Sensor.Service.BDMS_ST_STBPRP_BService;
import com.jhhc.WaterQuality.BaseInfo.service.BDMS_YB_FORECASTSCHEME_BService;
import com.jhhc.WaterQuality.BaseInfo.service.BDMS_YB_FORECASTSTATION_BService;
import com.jhhc.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


@Api(value="EngineeringSafetyBaseInfoController",tags={"预报基础信息"})
@RequestMapping(value = "EngineeringSafety/BaseInfo",headers = "Accept=application/json")
@RestController("EngineeringSafetyBaseInfoController")
public class BaseInfoController {

    @Autowired
    private BDMS_YB_FORECASTSCHEME_BService bdms_yb_forecastscheme_bService;
    @Autowired
    private BDMS_YB_FORECASTSTATION_BService bdms_yb_forecaststation_bService;
    @Autowired
    private BDMS_ST_STBPRP_BService bdms_st_stbprp_bService;
    @Autowired
    private BDMS_ES_FORECASTOBJ_BService bdms_es_forecastobj_bService;

    //获取预报工程信息
    @ApiOperation("获取监测工程信息")
    @RequestMapping(value = "GetEngineeringInfo", headers = "Accept=application/json", method = RequestMethod.GET)
    public JsonResult getForecastBaseInfos(@RequestParam Integer projectId) {
        JsonResult jsonResult = new JsonResult();
        List<EngineeringInfo> engineeringInfos =new ArrayList<>();
        //
        List<BDMS_ES_FORECASTOBJ_B> es_forecastobj_bs= bdms_es_forecastobj_bService.Qengineeringinfo(projectId);
        for (BDMS_ES_FORECASTOBJ_B es_forecastobj_b : es_forecastobj_bs) {
            EngineeringInfo engineeringInfo = new EngineeringInfo();
            engineeringInfo.setEngineeringId(es_forecastobj_b.getEngid());
            engineeringInfo.setEngineeringName(es_forecastobj_b.getEng());
            engineeringInfos.add(engineeringInfo);
        }

        jsonResult.setData(engineeringInfos);
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        jsonResult.setRemark("请求成功");

        return jsonResult;
    }



    //获取监测主体信息  monitoringSubject
    @ApiOperation("获取监测主体信息")
    @RequestMapping(value = "GetMonitoringSubjectInfo", headers = "Accept=application/json", method = RequestMethod.GET)
    public JsonResult getMonitoringSubject(@RequestParam Integer projectId,@RequestParam String engineeringId) {
        JsonResult jsonResult = new JsonResult();
        List<MonitoringSubjectInfo> monitoringSubjectInfos =new ArrayList<>();
        //
        List<BDMS_ES_FORECASTOBJ_B> es_forecastobj_bs= bdms_es_forecastobj_bService.QmonitoringSubject(projectId,engineeringId);
        for (BDMS_ES_FORECASTOBJ_B es_forecastobj_b : es_forecastobj_bs) {
            MonitoringSubjectInfo monitoringSubjectInfo = new MonitoringSubjectInfo();
            monitoringSubjectInfo.setMonitoringSubjectId(es_forecastobj_b.getObjforecastid());
            monitoringSubjectInfo.setMonitoringSubjectName(es_forecastobj_b.getObjforecast());
            monitoringSubjectInfos.add(monitoringSubjectInfo);
        }

        jsonResult.setData(monitoringSubjectInfos);
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        jsonResult.setRemark("请求成功");
        return jsonResult;
    }

    //获取监测要素信息
    @ApiOperation("获取监测要素信息")
    @RequestMapping(value = "GetMonitoringElementInfo", headers = "Accept=application/json", method = RequestMethod.GET)
    public JsonResult getMonitoringElement(@RequestParam Integer projectId,@RequestParam String engineeringId,@RequestParam String monitoringSubjectId) {
        JsonResult jsonResult = new JsonResult();
        List<MonitoringElementInfo> monitoringElementInfos =new ArrayList<>();
        //
        List<BDMS_ES_FORECASTOBJ_B> es_forecastobj_bs= bdms_es_forecastobj_bService.QmonitoringElement(projectId,engineeringId,monitoringSubjectId);
        //根据modelid分组 套位移或渗压

        // 根据modelid字段分组
        Map<String, List<BDMS_ES_FORECASTOBJ_B>> groupedByModelId = es_forecastobj_bs.stream()
                .collect(Collectors.groupingBy(BDMS_ES_FORECASTOBJ_B::getModelid));

        // 打印分组结果
        groupedByModelId.forEach((modelId, objects) -> {
            System.out.println(modelId);
            MonitoringElementInfo monitoringElementInfo = new MonitoringElementInfo();
            List<MonitoringElement> list = new ArrayList<>();
            if (Objects.equals(modelId,"1")){
                monitoringElementInfo.setModelType("位移");
            }else {
                monitoringElementInfo.setModelType("渗压");
            }

            for (BDMS_ES_FORECASTOBJ_B object : objects) {
                MonitoringElement monitoringElement = new MonitoringElement();
                monitoringElement.setElementId(object.getMonitortypeid());
                if (!Objects.equals(object.getMonitortypeid(),"1301")) {
                    monitoringElement.setElementName(monitoringElementInfo.getModelType() + "_" + object.getMonitortype());
                }else {
                    monitoringElement.setElementName( object.getMonitortype());
                }
                list.add(monitoringElement);
            }
            monitoringElementInfo.setMonitoringElements(list);
            monitoringElementInfos.add(monitoringElementInfo);
        });

//
//        for (BDMS_ES_FORECASTOBJ_B es_forecastobj_b : es_forecastobj_bs) {
//            MonitoringElementInfo monitoringElementInfo = new MonitoringElementInfo();
//            monitoringElementInfo.setElementId(es_forecastobj_b.getMonitortypeid());
//            monitoringElementInfo.setElementName(es_forecastobj_b.getMonitortype());
//            monitoringElementInfos.add(monitoringElementInfo);
//        }

        jsonResult.setData(monitoringElementInfos);
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        jsonResult.setRemark("请求成功");
        return jsonResult;
    }
    //获取监测点位信息
    @ApiOperation("获取监测点位信息")
    @RequestMapping(value = "GetMonitoringPointInfo", headers = "Accept=application/json", method = RequestMethod.GET)
    public JsonResult getMonitoringPointInfo(@RequestParam Integer projectId,@RequestParam String engineeringId,
                                             @RequestParam String monitoringSubjectId,@RequestParam String elementId) {
        JsonResult jsonResult = new JsonResult();
        List<MonitoringPointInfo> monitoringPointInfos =new ArrayList<>();
        //
        List<BDMS_ES_FORECASTOBJ_B> es_forecastobj_bs= bdms_es_forecastobj_bService.QmonitoringPonit(projectId,engineeringId,monitoringSubjectId,elementId);
        for (BDMS_ES_FORECASTOBJ_B es_forecastobj_b : es_forecastobj_bs) {
            MonitoringPointInfo monitoringPointInfo = new MonitoringPointInfo();
            monitoringPointInfo.setMonitorId(es_forecastobj_b.getMonitorid());
            monitoringPointInfo.setMonitorName(es_forecastobj_b.getMonitorname());
            monitoringPointInfo.setMonitorXY(es_forecastobj_b.getMonitorxy());
            monitoringPointInfo.setMonitorZ(es_forecastobj_b.getMonitorz());
           // monitoringPointInfo.setMonitorCode(es_forecastobj_b.getMonitorcode());
            monitoringPointInfos.add(monitoringPointInfo);
        }

        jsonResult.setData(monitoringPointInfos);
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        jsonResult.setRemark("请求成功");
        return jsonResult;
    }


    @ApiOperation("获取监测点位置信区间信息")
    @RequestMapping(value = "GetMonitoringConfidenceIntervalInfo", headers = "Accept=application/json", method = RequestMethod.GET)
    public JsonResult getMonitoringConfidenceIntervalInfo(@RequestParam Integer projectId,@RequestParam String engineeringId,
                                             @RequestParam String monitoringSubjectId,@RequestParam String elementId,@RequestParam(required = false) String monitorId) {
        JsonResult jsonResult = new JsonResult();
        List<MonitoringPointInfo> monitoringPointInfos =new ArrayList<>();
        //
        if (!"1202".equals(elementId)) {
            List<BDMS_ES_FORECASTOBJ_B> es_forecastobj_bs = bdms_es_forecastobj_bService.QmonitoringPonitQJ(projectId, engineeringId, monitoringSubjectId, elementId, monitorId);
            for (BDMS_ES_FORECASTOBJ_B es_forecastobj_b : es_forecastobj_bs) {
                MonitoringPointInfo monitoringPointInfo = new MonitoringPointInfo();
                monitoringPointInfo.setMonitorId(es_forecastobj_b.getMonitorid());
                monitoringPointInfo.setMonitorName(es_forecastobj_b.getMonitorname());
                monitoringPointInfo.setMonitorXY(es_forecastobj_b.getMonitorxy());
                monitoringPointInfo.setMonitorZ(es_forecastobj_b.getMonitorz());
//            if ("1202".equals(elementId)){
//                String xy [] = es_forecastobj_b.getRemark().split(",");
//                double x = Double.parseDouble(xy[0]);
//                double y = Double.parseDouble(xy[1]);
//                monitoringPointInfo.setMonitorConfidenceInterval(x*0.03+","+y*0.03);
//            }else {
                double v = Double.parseDouble(es_forecastobj_b.getRemark());
                System.out.println(v);
                double v1 = v - v * 0.03;
                System.out.println(v1);
                double v2 = v + v * 0.03;
                System.out.println(v2);
                monitoringPointInfo.setMonitorConfidenceInterval(v1 + "," + v2);
                // }

                monitoringPointInfos.add(monitoringPointInfo);
            }

            jsonResult.setData(monitoringPointInfos);
            jsonResult.setSucceeded(true);
            jsonResult.setStatusCode(200);
            jsonResult.setRemark("请求成功");
        }else {
            jsonResult.setData(null);
            jsonResult.setSucceeded(true);
            jsonResult.setStatusCode(200);
            jsonResult.setRemark("水平位移未提供监测点原始坐标");
        }
        return jsonResult;
    }


    //预报基础综合信息
    @ApiOperation("获取预报基础信息")
    @RequestMapping(value = "GetForecastBaseInfo", headers = "Accept=application/json", method = RequestMethod.GET)
    public JsonResult getMonitoringPointInfo(@RequestParam Integer projectId){
        JsonResult jsonResult = new JsonResult();

        List< EngineeringInfoResult > engineeringInfoResults = new ArrayList<>();

        List<BDMS_ES_FORECASTOBJ_B> bdms_es_forecastobj_bs = bdms_es_forecastobj_bService.queryAllByPId(projectId);
        Map<String, Map<String, Map<String, List<BDMS_ES_FORECASTOBJ_B>>>> groupedData = bdms_es_forecastobj_bs.stream()
                .collect(Collectors.groupingBy(BDMS_ES_FORECASTOBJ_B::getEngid,
                        Collectors.groupingBy(BDMS_ES_FORECASTOBJ_B::getObjforecastid,
                                Collectors.groupingBy(BDMS_ES_FORECASTOBJ_B::getMonitortypeid))));
        // 打印每个分组的数据
        for (Map.Entry<String, Map<String, Map<String, List<BDMS_ES_FORECASTOBJ_B>>>> engEntry : groupedData.entrySet()) {
            String engid = engEntry.getKey();
            EngineeringInfoResult engineeringResult = new EngineeringInfoResult();
            List<MonitoringSubjectResult> monitoringSubjectResults = new ArrayList<>();
            engineeringResult.setEngineeringId(engid);
            Map<String, Map<String, List<BDMS_ES_FORECASTOBJ_B>>> objForecastMap = engEntry.getValue();
            for (Map.Entry<String, Map<String, List<BDMS_ES_FORECASTOBJ_B>>> objForecastEntry : objForecastMap.entrySet()) {
                String objforecastid = objForecastEntry.getKey();
                MonitoringSubjectResult monitoringSubjectResult = new MonitoringSubjectResult();
                monitoringSubjectResult.setMonitoringSubjectId(objforecastid);
                List<MonitoringElementResult> monitoringElementResults = new ArrayList<>();
                Map<String, List<BDMS_ES_FORECASTOBJ_B>> monitorTypeMap = objForecastEntry.getValue();
                for (Map.Entry<String, List<BDMS_ES_FORECASTOBJ_B>> monitorTypeEntry : monitorTypeMap.entrySet()) {
                    MonitoringElementResult monitoringElementResult = new MonitoringElementResult();
                    List<MonitoringPointResult> monitoringPointResults = new ArrayList<>();
                    String monitortypeid = monitorTypeEntry.getKey();
                    monitoringElementResult.setElementId(monitortypeid);
                    List<BDMS_ES_FORECASTOBJ_B> group = monitorTypeEntry.getValue();
                    for (BDMS_ES_FORECASTOBJ_B bdms_es_forecastobj_b : group) {
                        engineeringResult.setEngineeringName(bdms_es_forecastobj_b.getEng());
                        monitoringSubjectResult.setMonitoringSubjectName(bdms_es_forecastobj_b.getObjforecast());
                        if (Objects.equals(monitortypeid,"1201")||Objects.equals(monitortypeid,"1202")){
                            monitoringElementResult.setModelType("位移");
                            monitoringElementResult.setElementName("位移_"+bdms_es_forecastobj_b.getMonitortype());
                        }else if (Objects.equals(monitortypeid,"1301")){
                            monitoringElementResult.setModelType("渗压");
                            monitoringElementResult.setElementName(bdms_es_forecastobj_b.getMonitortype());
                        }
                        MonitoringPointResult monitoringPointResult = new MonitoringPointResult();
                        monitoringPointResult.setMonitorXY(bdms_es_forecastobj_b.getMonitorxy());
                        monitoringPointResult.setMonitorZ(bdms_es_forecastobj_b.getMonitorz());
                        monitoringPointResult.setMonitorId(bdms_es_forecastobj_b.getMonitorid());
                        //monitoringPointResult.setMonitorCode(bdms_es_forecastobj_b.getMonitorcode());
                        monitoringPointResult.setMonitorName(bdms_es_forecastobj_b.getMonitorname());
                        monitoringPointResults.add(monitoringPointResult);
                    }
                    monitoringElementResult.setMonitoringPointInfos(monitoringPointResults);
                    monitoringElementResults.add(monitoringElementResult);
                }
                monitoringSubjectResult.setMonitoringElementInfos(monitoringElementResults);
                monitoringSubjectResults.add(monitoringSubjectResult);
            }
            engineeringResult.setMonitoringSubjectInfos(monitoringSubjectResults);
            engineeringInfoResults.add(engineeringResult);
        }

        jsonResult.setData(engineeringInfoResults);
        jsonResult.setRemark("请求成功");
        jsonResult.setSucceeded(true);
        jsonResult.setStatusCode(200);
        return jsonResult;
    }


}
